﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Ruminate.GUI;
using Ruminate.GUI.Content;
using Ruminate.GUI.Framework;
using System.Diagnostics;

namespace SimpleGame
{

    public class GameMap : Screen
    {
        SpriteBatch spriteBatch;
        Vector2 scrollPos;
        List<Building> buildingList;

        Texture2D mapTexture;
        Vector2 mapSize;
        MouseState _oldMouse;

        public bool ShowEmptySpace=false;

        Vector2 ScreenTopLeft;
        Vector2 ScreenBottomRight;
        Vector2 ScreenRect;

        bool isDrag = false;

        public GameMap()
        {
            scrollPos.X = 0;
            scrollPos.Y = 0;
            buildingList = new List<Building>();

            
        }

        public override void Init(Game1 game)
        {
            _game = game;
            spriteBatch = _game.spriteBatch;

            UpdateScreenRect();

        }

        public override void OnResize()
        {
            throw new NotImplementedException();
        }

        public void UpdateScreenRect()
        {
            Tuple<Vector2, Vector2> position = _game.mainUI.getScreenRect();
            ScreenTopLeft = position.Item1;
            ScreenBottomRight = position.Item2;
            ScreenRect = ScreenBottomRight - ScreenTopLeft;
        }

        public void LoadMap(Texture2D mapTexture, List<Building> buildingList)
        {
            //TODO : LoadMap 확장

            Debug.Assert(mapTexture != null);
            this.mapTexture = mapTexture;
            this.buildingList = buildingList;

            mapSize.X = mapTexture.Width;
            mapSize.Y = mapTexture.Height;

            Console.WriteLine(mapSize);

            Vector2 initPos;
            initPos.X = -mapSize.X / 2 + (ScreenBottomRight.X-ScreenTopLeft.X) / 2 + ScreenTopLeft.X;
            initPos.Y = -mapSize.Y / 2 + (ScreenBottomRight.Y - ScreenTopLeft.Y) / 2 + ScreenTopLeft.Y;

            moveMapAbs(initPos);

            _game.mainUI.changeMode(true);
        }

        public void moveMapAbs(Vector2 targetPos)
        {
            Vector2 TLAlign = targetPos - ScreenTopLeft;
            Vector2 BRAlign = ScreenBottomRight - targetPos;

            if (ScreenBottomRight.X - ScreenTopLeft.X > mapSize.X)
            {
                //이건 맵이 작은거
                targetPos.X = -mapSize.X / 2 + ScreenRect.X / 2 + ScreenTopLeft.X;
            }
            else if (TLAlign.X > 0)
            {
                targetPos.X = ScreenTopLeft.X;
            }
            else if (BRAlign.X > mapSize.X)
            {
                targetPos.X = -mapSize.X + ScreenBottomRight.X;
            }

            if (ScreenBottomRight.Y - ScreenTopLeft.Y > mapSize.Y)
            {
                //이건 맵이 작은거
                targetPos.Y = -mapSize.Y / 2 + ScreenRect.Y / 2 + ScreenTopLeft.Y;
            }
            else if (TLAlign.Y > 0)
            {
                targetPos.Y = ScreenTopLeft.Y;
            }
            else if (BRAlign.Y > mapSize.Y)
            {
                targetPos.Y = -mapSize.Y + ScreenBottomRight.Y;
            }

            scrollPos = targetPos;
        }

        public void moveMap(Vector2 move)
        {
            Vector2 targetPos = scrollPos + move;

            moveMapAbs(targetPos);
        }

        public override bool Update(GameTime gameTime)
        {
            MouseState ms = Mouse.GetState();
            MouseState oldMouse = _oldMouse;

            bool hasMouse = _game.mainUI.hasMouse;
            if (_oldMouse == ms)
            {
                return false;
            }
            _oldMouse = ms;
            if (oldMouse == null)
            {
                return false;
            }


            if (isDrag)
            {
                moveMap(new Vector2(ms.X - oldMouse.X,ms.Y - oldMouse.Y));
            }

            

            if(ms.LeftButton == ButtonState.Pressed){

                if (ms.X > ScreenBottomRight.X || ms.Y > ScreenBottomRight.Y || ms.X < ScreenTopLeft.X || ms.X < ScreenTopLeft.Y || isDrag || hasMouse)
                {

                    return false;
                }

                Vector2 mousePos = new Vector2(ms.X,ms.Y);
                foreach (Building building in buildingList)
                {
                    if (building.Update(gameTime, scrollPos, mousePos))
                    {
                        return true;
                    }
                }
                isDrag = true;
            }
            else if (ms.LeftButton == ButtonState.Released)
            {
                if (isDrag)
                {
                    isDrag = false;
                }
            }
            return false;
            
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(mapTexture, scrollPos, Color.White);

            if (buildingList != null && buildingList.Count > 0)
            {
                foreach (Building building in buildingList)
                {
                    if (building.GetType() == typeof(BuildingEmpty))
                    {
                        if (ShowEmptySpace)
                        {
                            building.Draw(gameTime, scrollPos);
                        }
                        
                    }
                    else
                    {
                        building.Draw(gameTime, scrollPos);
                    }
                    
                }
            }
            spriteBatch.End();
        }
    }
}
